from django.db import models
from django.utils import timezone 
from django.contrib.auth.models import User
# Create your models here.

class PublishedManager(models.Manager):
	def get_queryset(self):
		return super(PublishedManager,self).get_queryset().filter(status='publish')


class Post(models.Model):
	STATUS_CHOICES = (
		('draft','Draft'),
		('publish','Publish')
	)
	
	title = models.CharField(max_length=250)
	slug = models.SlugField(max_length=250,unique_for_date='publish')
	author = models.ForeignKey(User,related_name='blog_posts')
	body = models.TextField()
	publish = models.DateTimeField(default=timezone.now)
	created = models.DateTimeField(auto_now_add=True)
	update = models.DateTimeField(auto_now=True)
	status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')

		
	class Meta:
			ordering = ('-publish',)
			# 也可以在这里指定表名
	def __str__(self):
		return self.title


class Comment(models.Model):
	post = models.ForeignKey(Post,related_name='comments')
	name = models.CharField(max_length=80)
	email = models.EmailField()
	body = models.TextField()
	created = models.DateTimeField(auto_now_add=True)
	updated = models.DateTimeField(auto_now=True)
	active = models.BooleanField(default=True)

	class Meta:
		ordering = ('created',)

	def __str__(self):
		return 'Comment by {} on {}'.format(self.name,self.post)